//
//  DatePickerWithoutYearViewController.m
//  DatePickerWithoutYear
//
//  Created by Ajay on 8/2/10.
//  Copyright Tap Inferno (www.tapinferno.com) 2010. All rights reserved.
//
// This project creates an iPhone custom UIPickerView, to replace a UIDatePicker, and contains only months and days

#import "DatePickerWithoutYearViewController.h"

@implementation DatePickerWithoutYearViewController

@synthesize monthNames;
@synthesize dateFormatter;

- (void)dealloc {
    [dateFormatter release];
    [monthNames release];
    [super dealloc];
}

#pragma mark -
#pragma mark Util Methods

-(NSInteger)daysForMonth:(NSInteger)monthIndex inYear:(NSString*)yearString {
    NSCalendar* cal = [NSCalendar currentCalendar];
    NSDateComponents* comps = [[[NSDateComponents alloc] init] autorelease];
    
    [comps setMonth:monthIndex+1];
    [comps setYear:[yearString intValue]];
    
    NSRange range = [cal rangeOfUnit:NSDayCalendarUnit
                              inUnit:NSMonthCalendarUnit
                             forDate:[cal dateFromComponents:comps]];
    
    return range.length;
}

#pragma mark -
#pragma mark View Management

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    self.monthNames = [dateFormatter monthSymbols];
    
    maxDaysForMonth = [self daysForMonth:0 inYear:yearTextField.text];
}

-(void) viewWillAppear:(BOOL)animated {
    [monthDayPicker selectRow:([monthNames count] * 1000) inComponent:0 animated:NO];
    [monthDayPicker selectRow:(maxDaysForMonth * 1000) inComponent:1 animated:NO];
}

#pragma mark -
#pragma mark User interaction

-(IBAction)setYear {
    [yearTextField resignFirstResponder];
    yearTextField.text = @"2010";
    [[[[UIAlertView alloc] initWithTitle:@"Not Implemented" message:@"" delegate:nil
                       cancelButtonTitle:@"Dismiss" otherButtonTitles:nil] autorelease] show];
}

-(IBAction)selectDate {
    [[[[UIAlertView alloc] initWithTitle:@"Too lazy to delete this button ;)" message:@"" delegate:nil
                       cancelButtonTitle:@"Dismiss" otherButtonTitles:nil] autorelease] show];
}

#pragma mark -
#pragma mark Picker management

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)unused1 inComponent:(NSInteger)component {
    NSInteger selectedMonthRow = [pickerView selectedRowInComponent:0] % [monthNames count];
    NSInteger selectedDayIndex = [pickerView selectedRowInComponent:1] % maxDaysForMonth;
    if (component == 0) {
        maxDaysForMonth = [self daysForMonth:selectedMonthRow inYear:yearTextField.text];
        [pickerView reloadComponent:1];
        [monthDayPicker selectRow:((maxDaysForMonth * 1000) + selectedDayIndex) inComponent:1 animated:NO];
    }
    
    selectedDayIndex = [pickerView selectedRowInComponent:1] % maxDaysForMonth;
    NSString *dateStr = [NSString stringWithFormat:@"%@ %d, %@",
                         [monthNames objectAtIndex:selectedMonthRow],
                         selectedDayIndex + 1,
                         yearTextField.text];
    selectedDate.text = dateStr;
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 2;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    switch (component) {
        case 0:
            return 180;
            break;
            
        case 1:
            return 140;
            break;
    }
    
    return 0;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return NSIntegerMax;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    switch (component) {
        case 0:
            return [monthNames objectAtIndex:(row % [monthNames count])];
            break;
            
        case 1: {
            NSInteger dayNo = (row % maxDaysForMonth) + 1;
            return [NSString stringWithFormat:@"%d", dayNo];
            break;
        }
    }
    return nil;
    
}

//- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
//    CGRect frame = CGRectMake(0, 0,
//                              [self pickerView:pickerView widthForComponent:component],
//                              44);
//    UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease];
//    
//    [label setText:[self pickerView:pickerView titleForRow:row forComponent:component]];
//    [label setFont:[UIFont boldSystemFontOfSize:14]];
//    
//    if (component == 0) {
//        [label setTextAlignment:UITextAlignmentRight];
//    }
//    return label;
//}

@end
